Imports DataAccessLayer
Imports DataAccessLayer.DLMain
Imports DataAccessLayer.DLWriter
Imports UniversalLayer
Imports UniversalLayer.ULMain
Imports UniversalLayer.ULConverter

Public Class BLMain

    ''' <summary>
    ''' NO COMMENTS
    ''' </summary>
    ''' <remarks></remarks>
    Public Enum eInitMode
        Validation = 0
        Settings = 1
        Actions = 2
    End Enum

    ''Public Enum eReleaseYear
    ''    yMinimum = yMinimumRelYr '1800
    ''    yDefault = yDefaultRelYr '1900
    ''    yMaximum = yMaximumRelYr '2100
    ''End Enum

    ''Public Enum eNumDiscs
    ''    nMinimum = nMinimumDiscs '0
    ''    nDefault = nDefaultDiscs '0
    ''    nMaximum = nMaximumDiscs '10
    ''End Enum

    ''Public Enum eRuntime
    ''    rMinimum = rMinimumRunTime '0
    ''    rDefault = rDefaultRunTime '0
    ''    rMaximum = rMaximumRunTime '250
    ''End Enum

    Public Enum eDefaultWebsites
        wOfficialSite = 0
        wIMDbLink = 1
        wWikiLink = 2
        wIMDbTrailerLink = 3
    End Enum

#Region "Declarations"

    'Const sEncryptionKey As String = "MuveeManager_EncryptionKey_IamRKD0123456789"
    Dim _BLRegex As BLRegex
    Dim defIMDbCode As String = "tt0000000"
    Dim defOfficial As String = ""
    Dim defIMDbLink As String = "http://www.imdb.com/title/tt0000000/"
    Dim defWikiLink As String = "http://en.wikipedia.org/"
    Dim defIMDbTrailerLink As String = "http://www.imdb.com/title/tt0000000/trailers/"

#End Region

#Region "Declarations : Settings"

    Public iMinimumRelYr, iDefaultRelYr, iMaximumRelYr As Integer
    Public iMinimumDiscs, iDefaultDiscs, iMaximumDiscs As Integer
    Public iMinimumRunTime, iDefaultRunTime, iMaximumRunTime As Integer
    Public iMimimumTVSeason, iDefaultTVSeason, iMaximumTVSeason As Integer
    Public iMimimumTVEpisode, iDefaultTVEpisode, iMaximumTVEpisode As Integer
    Private dtSettingsMain As DataTable

#End Region

    Public Sub New(ByVal iInitMode As eInitMode)
        Dim _DLSettings As DataAccessLayer.DLSettings
        Try
            _DLSettings = New DataAccessLayer.DLSettings
            dtSettingsMain = _DLSettings.FetchAllSettings

            Select Case iInitMode
                Case eInitMode.Validation 'Do Nothing
                Case eInitMode.Actions 'Do Nothing

                Case eInitMode.Settings
                    Dim dtSettingsRange As DataTable
                    Try
                        dtSettingsRange = CType(_DLSettings.FetchSettings("", "", "Meta IN ('nDiscs','ReleaseYear','RunTime','TVEpisode','TVSeason') AND ID IN ('Minimum','Default','Maximum')", DLSettings.eSettingsReturnType.DataTable), DataTable)

                        'dtSettings = CType(_DLSettings.FetchSettings("ReleaseYear", "", "ID IN ('Minimum','Default','Maximum')", DLSettings.eSettingsReturnType.DataTable), DataTable)
                        If dtSettingsRange.Select("Meta='ReleaseYear'").Length = 3 Then
                            iMinimumRelYr = CInt(dtSettingsRange.Select("Meta='ReleaseYear' AND ID='" & eRangeType.Minimum.ToString & "'")(0).Item("Value").ToString) '1800
                            iDefaultRelYr = CInt(dtSettingsRange.Select("Meta='ReleaseYear' AND ID='" & eRangeType.Default.ToString & "'")(0).Item("Value").ToString) '1900
                            iMaximumRelYr = CInt(dtSettingsRange.Select("Meta='ReleaseYear' AND ID='" & eRangeType.Maximum.ToString & "'")(0).Item("Value").ToString) '2100
                        End If
                        'dtSettings = Nothing

                        'dtSettings = CType(_DLSettings.FetchSettings("nDiscs", "", "ID IN ('Minimum','Default','Maximum')", DLSettings.eSettingsReturnType.DataTable), DataTable)
                        If dtSettingsRange.Select("Meta='nDiscs'").Length = 3 Then
                            iMinimumDiscs = CInt(dtSettingsRange.Select("Meta='nDiscs' AND ID='" & eRangeType.Minimum.ToString & "'")(0).Item("Value").ToString) '0
                            iDefaultDiscs = CInt(dtSettingsRange.Select("Meta='nDiscs' AND ID='" & eRangeType.Default.ToString & "'")(0).Item("Value").ToString) '0
                            iMaximumDiscs = CInt(dtSettingsRange.Select("Meta='nDiscs' AND ID='" & eRangeType.Maximum.ToString & "'")(0).Item("Value").ToString) '10
                        End If
                        'dtSettings = Nothing

                        'dtSettings = CType(_DLSettings.FetchSettings("RunTime", "", "ID IN ('Minimum','Default','Maximum')", DLSettings.eSettingsReturnType.DataTable), DataTable)
                        If dtSettingsRange.Select("Meta='RunTime'").Length = 3 Then
                            iMinimumRunTime = CInt(dtSettingsRange.Select("Meta='RunTime' AND ID='" & eRangeType.Minimum.ToString & "'")(0).Item("Value").ToString) '0
                            iDefaultRunTime = CInt(dtSettingsRange.Select("Meta='RunTime' AND ID='" & eRangeType.Default.ToString & "'")(0).Item("Value").ToString) '0
                            iMaximumRunTime = CInt(dtSettingsRange.Select("Meta='RunTime' AND ID='" & eRangeType.Maximum.ToString & "'")(0).Item("Value").ToString) '250
                        End If
                        'dtSettings = Nothing

                        'dtSettings = CType(_DLSettings.FetchSettings("TVSeason", "", "ID IN ('Minimum','Default','Maximum')", DLSettings.eSettingsReturnType.DataTable), DataTable)
                        If dtSettingsRange.Select("Meta='TVSeason'").Length = 3 Then
                            iMimimumTVSeason = CInt(dtSettingsRange.Select("Meta='TVSeason' AND ID='" & eRangeType.Minimum.ToString & "'")(0).Item("Value").ToString) '1
                            iDefaultTVSeason = CInt(dtSettingsRange.Select("Meta='TVSeason' AND ID='" & eRangeType.Default.ToString & "'")(0).Item("Value").ToString) '1
                            iMaximumTVSeason = CInt(dtSettingsRange.Select("Meta='TVSeason' AND ID='" & eRangeType.Maximum.ToString & "'")(0).Item("Value").ToString) '20
                        End If
                        'dtSettings = Nothing

                        'dtSettings = CType(_DLSettings.FetchSettings("TVEpisode", "", "ID IN ('Minimum','Default','Maximum')", DLSettings.eSettingsReturnType.DataTable), DataTable)
                        If dtSettingsRange.Select("Meta='TVEpisode'").Length = 3 Then
                            iMimimumTVEpisode = CInt(dtSettingsRange.Select("Meta='TVEpisode' AND ID='" & eRangeType.Minimum.ToString & "'")(0).Item("Value").ToString) '1
                            iDefaultTVEpisode = CInt(dtSettingsRange.Select("Meta='TVEpisode' AND ID='" & eRangeType.Default.ToString & "'")(0).Item("Value").ToString) '1
                            iMaximumTVEpisode = CInt(dtSettingsRange.Select("Meta='TVEpisode' AND ID='" & eRangeType.Maximum.ToString & "'")(0).Item("Value").ToString) '50
                        End If
                        'dtSettings = Nothing

                    Finally
                        dtSettingsRange = Nothing
                    End Try
            End Select
        Finally
            _DLSettings = Nothing
        End Try
    End Sub

    ''' <summary>
    ''' Displays a message to the user
    ''' </summary>
    ''' <param name="sMessage">Message to be displayed</param>
    ''' <param name="msgStyle">Message Box style</param>
    ''' <remarks></remarks>
    Public Sub NotifyUser(ByVal sMessage As String, Optional ByVal msgStyle As MsgBoxStyle = MsgBoxStyle.OkOnly)
        Dim sTitle As String = ""

        Select Case msgStyle
            Case MsgBoxStyle.Critical
                sTitle = ""
            Case MsgBoxStyle.Information
                sTitle = ""
            Case MsgBoxStyle.OkCancel
                sTitle = ""

        End Select

        MsgBox(sMessage, msgStyle, sTitle)
    End Sub

#Region "Validations"

#Region "Others"

    '''' <summary>
    '''' NOT COMPLETED
    '''' </summary>
    '''' <param name="sCondition"></param>
    '''' <param name="sValueTrue"></param>
    '''' <returns></returns>
    '''' <remarks></remarks>
    'Public Function IIf(ByVal sCondition As Boolean, ByVal sValueTrue As Object) As Object
    '    'NOT developed
    '    Return Nothing
    'End Function

    ''' <summary>
    ''' NO COMMENTS
    ''' </summary>
    ''' <param name="_DLEntertainment"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function Validate(ByRef _DLEntertainment As DLEntertainment) As Boolean

        Try

            'RKD: Also check for DB/SQL validations
            With _DLEntertainment
                '=-=-=-=-=-=-=-=-=-=-=-=-= Movie Info Tab
                If IsEmpty(.cTitle) Then Return False
                If IsEmpty(.cAlternateTitle) Then .cAlternateTitle = .cTitle
                If IsEmpty(.cRatingIMDb) Or Not IsNumeric(.cRatingIMDb) Then .cRatingIMDb = 0
                If IsEmpty(.cRatingCustom) Or Not IsNumeric(.cRatingCustom) Then .cRatingCustom = 0

                '=-=-=-=-=-=-=-=-=-=-=-=-= Main Details Tab
                If Not IsFileExists(.cPosterLocation) Then .cPosterLocation = ""
                '.POSTER=""
                If Not IsNumeric(.cSerialNo) Then Return False
                If Not IsNumeric(.cReleaseYear) Or .cReleaseYear < iMinimumRelYr Or _
                    .cReleaseYear > iMaximumRelYr Then .cReleaseYear = iDefaultRelYr
                .cSeen = .cSeen
                If Not IsNumeric(.cRunTimeMins) Then .cRunTimeMins = 0
                If Not IsNumeric(.cNDiscs) Or .cNDiscs < iMinimumDiscs Or _
                    .cNDiscs > iMaximumDiscs Then .cNDiscs = iDefaultDiscs

                .cDirector = .cDirector
                .cCountry = .cCountry
                .cGenre = .cGenre
                .cClassification = .cClassification
                .cStudio = .cStudio

                If InStr(.cLinkIMDb, "http://wwww.imdb.com/tt", CompareMethod.Text) = 0 Then .cIMDbCode = defIMDbCode _
                    Else .cIMDbCode = Mid(Replace(.cLinkIMDb, "/", ""), .cLinkIMDb.Length - defIMDbCode.Length + 1, defIMDbCode.Length)
                If Not _BLRegex.RegexValidator(BLRegex.eFormat.eURL, .cLinkOfficial) Then .cLinkOfficial = defOfficial
                If Not _BLRegex.RegexValidator(BLRegex.eFormat.eURL, .cLinkIMDb) Then .cLinkOfficial = defOfficial
                '.LINK_IMDB = txtIMDbLink.Text 'check regex, else default
                '.LINK_WIKI = txtWikiLink.Text 'check regex, else default
                '.LINK_TRAILER = txtTrailer.Text 'check regex, else default

                '=-=-=-=-=-=-=-=-=-=-=-=-= Technical Info Tab 'RKD: Conversion of list to array
                '.MEDIUM = CInt(cmbMedium.SelectedValue)
                '.nDISCs = CInt(numNDiscs.Value)
                '.CONDITION = CInt(cmbCondition.SelectedValue)
                '.DVD_REGION = CInt(cmbDvdRegion.SelectedValue)
                '.DVD_LAYER = CInt(cmbDvdLayer.SelectedValue)
                '.VOLUME = CInt(cmbVolume.SelectedValue)
                '.COLLECTION = CInt(cmbCollection.SelectedValue)
                '.COLOUR = CInt(cmbColour.SelectedValue)
                '.V_CODEC = CInt(cmbVCodec.SelectedValue)

                '=-=-=-=-=-=-=-=-=-=-=-=-= Languages Tab

                '=-=-=-=-=-=-=-=-=-=-=-=-= Plot/Cast/Notes Tab
                '.PLOT = txtPlot.Text
                '.CAST = txtCast.Text
                '.NOTES = txtNotes.Text

            End With
            Return True

        Catch ex As Exception
            Return False

        End Try

    End Function

#End Region


#End Region






  

    ''' <summary>
    ''' NO COMMENTS
    ''' </summary>
    ''' <param name="sEntertainmentMainPKey">EntertainmentMain.PKey</param>
    ''' <param name="dateWatched"></param>
    ''' <remarks></remarks>
    Public Sub WatchlistToday(ByVal sEntertainmentMainPKey As String, Optional ByVal dateWatched As Date = Nothing)
        Dim _DLWriter As DataAccessLayer.DLWriter
        Dim sSQL As String

        Try
            _DLWriter = New DataAccessLayer.DLWriter("BLMain")
            If Not IsEmpty(sEntertainmentMainPKey) OrElse dateWatched.Date = Today.Date Then
                If IsEmpty(dateWatched) Then
                    'sSQL = "IF EXISTS (SELECT 1 FROM WatchlistMain WHERE EntertainmentMainPKey = '" & txtPreviewPKey.Text & "' AND DateWatched IS NULL) " & _
                    '       " UPDATE WatchlistMain SET DateWatched = current_timestamp WHERE EntertainmentMainPKey = '" & txtPreviewPKey.Text & "' AND DateWatched IS NULL AND Status <> 'd' " & _
                    '       "ELSE INSERT INTO WatchlistMain(EntertainmentMainPKey, DateAdded, DateWatched) VALUES('" & txtPreviewPKey.Text & "', '" & Now.Date & "', '" & Now & "')"
                    sSQL = "IF EXISTS (SELECT 1 FROM WatchlistMain WHERE EntertainmentMainPKey = '" & sEntertainmentMainPKey & "' AND DateWatched IS NULL) " & _
                           " UPDATE WatchlistMain SET DateWatched = current_timestamp WHERE EntertainmentMainPKey = '" & sEntertainmentMainPKey & "' AND DateWatched IS NULL AND Status <> 'd' AND " & _
                           "    PKey = (SELECT TOP 1 PKey FROM WatchlistMain WHERE EntertainmentMainPKey = '" & sEntertainmentMainPKey & "' ORDER BY PKey DESC) " & _
                           "ELSE INSERT INTO WatchlistMain(EntertainmentMainPKey, DateAdded, DateWatched) VALUES('" & sEntertainmentMainPKey & "', convert(varchar,current_timestamp,110), current_timestamp)"
                Else
                    sSQL = "IF EXISTS (SELECT 1 FROM WatchlistMain WHERE EntertainmentMainPKey = '" & sEntertainmentMainPKey & "' AND DateWatched IS NULL) " & _
                           " UPDATE WatchlistMain SET DateWatched = " & dateWatched & " WHERE EntertainmentMainPKey = '" & sEntertainmentMainPKey & "' AND DateWatched IS NULL AND Status <> 'd' AND " & _
                           "    PKey = (SELECT TOP 1 PKey FROM WatchlistMain WHERE EntertainmentMainPKey = '" & sEntertainmentMainPKey & "' ORDER BY PKey DESC) " & _
                           "ELSE INSERT INTO WatchlistMain(EntertainmentMainPKey, DateAdded, DateWatched) VALUES('" & sEntertainmentMainPKey & "', convert(varchar," & dateWatched & ",110), " & dateWatched & ")"
                End If
                If Not IsEmpty(sSQL) Then Call _DLWriter.ExecuteTransactSQL(sSQL, eTransactSQL.Update)
            End If

        Finally
            sSQL = Nothing
            _DLWriter = Nothing
        End Try
    End Sub



  



   




    ''Public Sub TestCrypto()
    ''    Dim oCrypto As New Cryptography.Cryptography
    ''    Dim sCrypt As String

    ''    sCrypt = oCrypto.EncryptTripleDES("IamRKD", sEncryptionKey)
    ''    MsgBox("'" & sCrypt & "'", , "Encrypted text")
    ''    MsgBox("'" & oCrypto.DecryptTripleDES(sCrypt, sEncryptionKey) & "'", , "Decrypted text")
    ''End Sub

    ''Public Function TestReturnCrypto(ByVal sText As String, ByVal sKey As String) As String
    ''    Dim oCrypto As New Cryptography.Cryptography

    ''    Return oCrypto.EncryptTripleDES(sText, sKey)
    ''End Function
End Class
